package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.MenuEntity;
import com.sq.universe.system.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
    * 查询数据
    * @param menuEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:menu:query")
    public RespEntity query(MenuEntity menuEntity){
        try {
            return new RespEntity("查询成功",menuService.query(menuEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param menuEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:menu:queryByPage")
    public RespEntity queryByPage(MenuEntity menuEntity){
        try {
            PageHelper.startPage(menuEntity.getPageNo(),menuEntity.getPageSize());
            List<MenuEntity> menuList = menuService.query(menuEntity);
            PageInfo<MenuEntity> pageInfo = new PageInfo<MenuEntity>(menuList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param menuEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:menu:insert")
    public RespEntity insert(MenuEntity menuEntity){
        try {
            menuEntity.setMenuCreateTime(new Date());
            menuEntity.setMenuCreateUser(this.getCurrentUser().getUserId());
            menuService.insert(menuEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param menuEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:menu:update")
    public RespEntity update(MenuEntity menuEntity){
        try {
            menuEntity.setMenuUpdateTime(new Date());
            menuEntity.setMenuUpdateUser(this.getCurrentUser().getUserId());
            menuService.update(menuEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param menuEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:menu:delete")
    public RespEntity delete(MenuEntity menuEntity){
        try {
            if(menuEntity.getMenuId() == null){
                return new RespEntity("删除失败");
            }
            menuEntity.setMenuUpdateTime(new Date());
            menuEntity.setMenuUpdateUser(this.getCurrentUser().getUserId());
            menuService.logicalDelete(menuEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }

    /**
     * 删除成功
     * @param menuEntity
     * @return
     */
    @PostMapping("/queryTree")
    @RequiresPermissions("system:menu:queryTree")
    public RespEntity queryTree(MenuEntity menuEntity){
        try {
            return new RespEntity("查询成功",menuService.queryTree(menuEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }
}